package com.message.api.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.base.helper.StringUtils;
import com.message.api.base.dao.AcConfigMapper;
import com.message.api.base.entity.AcConfig;
import com.message.api.base.service.AcConfigService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mzl
 * @since 2020-07-09
 */
@Service
public class AcConfigServiceImpl extends ServiceImpl<AcConfigMapper, AcConfig> implements AcConfigService {

    @Override
    public List<AcConfig> list(AcConfig example) {
        return this.list(buildWrapper(example));
    }

    @Override
    public IPage<AcConfig> page(AcConfig example, IPage page) {
        return this.page(page, buildWrapper(example));
    }

    @Override
    public AcConfig setConfigvalue(Integer id, String configvalue) {
        AcConfig acConfig = this.getById(id);
        if (null == acConfig) {
            throw new RuntimeException("没有找到id对应对象");
        }
//        if (configvalue.equals(acConfig.getConfigvalue())) {
//            throw new RuntimeException("该用户已经处于该状态");
//        }
        acConfig.setConfigvalue(configvalue);
        this.updateById(acConfig);
        return acConfig;
    }

    @Override
    public String getRegisterStatus() {
        AcConfig acConfig = this.getById(1);
        if (null == acConfig) {
            throw new RuntimeException("没有找到注册相关的配置");
        }
        return acConfig.getConfigvalue();
    }

    @Override
    public AcConfig findByConfigname(String configname) {
        return this.baseMapper.findByConfigname(configname);
    }

    @Override
    public AcConfig initEnrollNum(String configname, String configvalue) {
        AcConfig acConfig = this.findByConfigname(configname);
        if (null == acConfig) {
            acConfig = new AcConfig();
            acConfig.setConfigname(configname);
            acConfig.setConfigvalue(configvalue);
            super.save(acConfig);
        } else {
            acConfig.setConfigvalue(configvalue);
            super.updateById(acConfig);
        }
        return acConfig;
    }

    @Override
    public synchronized Integer findNumByEnrollid(String enrollid) {
        Integer  num      = null;
        AcConfig acConfig = this.findByConfigname(enrollid);
        if (null == acConfig) {
            acConfig = this.initEnrollNum(enrollid, "1");
        }
        num = Integer.parseInt(acConfig.getConfigvalue());
        acConfig.setConfigvalue((num + 1) + "");
        if (!super.updateById(acConfig)) {
            throw new RuntimeException("获取考生编号失败");
        }
        return num;
    }

    /**
     * 构建查询
     *
     * @param example
     * @return
     */
    private QueryWrapper<AcConfig> buildWrapper(AcConfig example) {
        QueryWrapper<AcConfig> wrapper     = new QueryWrapper<>();
        String                 configname  = example.getConfigname();
        String                 configvalue = example.getConfigvalue();
        wrapper.lambda()
                .like(StringUtils.isNotEmpty(configname), AcConfig::getConfigname, configname)
                .eq(StringUtils.isNotEmpty(configvalue), AcConfig::getConfigvalue, configvalue)
                .orderByDesc(AcConfig::getId);

        return wrapper;
    }

}
